﻿Imports System.Management
Imports System.Security.Cryptography
Imports System.Text

Namespace FL_8x0.Common

    Public Module MacUtil

        Public Function GetNetworkAdpaterId() As String
            Try
                Dim mac As String = ""
                Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")

                Dim moc As ManagementObjectCollection = mc.GetInstances()

                For Each mo As ManagementObject In moc
                    If CBool(mo("IPEnabled")) = True Then
                        mac &= mo("MacAddress").ToString() & " "
                        Exit For
                    End If
                Next mo
                Return mac.Trim()
            Catch e As Exception
                Return "B0:B0:B0:B0"
            End Try
        End Function



        Public Function GetMd5(ByVal strEncyp As String) As String
            Dim md5Conflict As MD5 = MD5.Create()
            '需要将字符串转成字节数组
            Dim buffer() As Byte = Encoding.Default.GetBytes("123")
            '加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择
            Dim md5Buffer() As Byte = md5Conflict.ComputeHash(buffer)
            Dim str As String = Nothing
            ' 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            For Each b As Byte In md5Buffer
                '得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                '但是在和对方测试过程中，发现我这边的MD5加密编码，经常出现少一位或几位的问题；
                '后来分析发现是 字符串格式符的问题， X 表示大写， x 表示小写， 
                'X2和x2表示不省略首位为0的十六进制数字；
                str &= b.ToString("x2")
            Next b
            Return str
        End Function


        Public Function GetId() As String
            Dim ID As Long = 0  'RID

            'Win32_DiskDrive方法读硬盘ID
            Try
                Dim info As ManagementBaseObject
                Dim search As New ManagementObjectSearcher("Select * FROM Win32_DiskDrive")

                For Each info In search.Get()
                    If info("serialnumber") IsNot Nothing Then
                        Dim tmp As String = info("serialnumber").ToString
                        tmp = Right(tmp, 8)

                        If Len(tmp) = 8 Then
                            If IsNumeric("&H" & tmp) Then
                                ID = CLng("&H" & tmp)

                                If ID > 0 Then
                                    Return Uint32ToHEX(ID) '返回RID
                                End If
                            End If
                        End If
                    End If
                Next
            Catch ex As Exception
                'MsgBox(ex.Message, vbOKOnly, ex.Source)
                'Return ""
            End Try

            '注册表方法读硬盘ID
            Try
                Dim Identifier As String = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\Scsi Port 0\Scsi Bus 0\Target Id 0\Logical Unit Id 0", "Identifier", "")
                Dim tmp As String = Right(Identifier, 8)

                If Len(tmp) = 8 Then
                    If IsNumeric("&H" & tmp) Then
                        ID = CLng("&H" & tmp)

                        If ID > 0 Then
                            Return Uint32ToHEX(ID) '返回RID
                        End If
                    End If
                End If

            Catch ex As Exception
                'MsgBox(ex.Message, vbOKOnly, ex.Source)
                'Return ""
            End Try

            'Win32_Processor方法读CPU的ID
            Try
                Dim info As ManagementBaseObject
                Dim search As New ManagementObjectSearcher("Select * FROM Win32_Processor")

                For Each info In search.Get
                    If info("ProcessorId") IsNot Nothing Then
                        Dim tmp As String = info("ProcessorId").ToString
                        tmp = Right(tmp, 8)

                        If Len(tmp) = 8 Then
                            If IsNumeric("&H" & tmp) Then
                                ID = CLng("&H" & tmp)

                                If ID > 0 Then
                                    Return Uint32ToHEX(ID) '返回RID
                                End If
                            End If
                        End If
                    End If
                Next
            Catch ex As Exception
                'MsgBox(ex.Message, vbOKOnly, ex.Source)
                'Return ""
            End Try

            'Win32_BaseBoard方法读主板ID
            Try
                Dim info As ManagementBaseObject
                Dim search As New ManagementObjectSearcher("Select * FROM Win32_BaseBoard")

                For Each info In search.Get
                    If info("serialnumber") IsNot Nothing Then
                        Dim tmp As String = info("serialnumber").ToString
                        tmp = Right(tmp, 8)

                        If Len(tmp) = 8 Then
                            If IsNumeric("&H" & tmp) Then
                                ID = CLng("&H" & tmp)

                                If ID > 0 Then
                                    Return Uint32ToHEX(ID) '返回RID
                                End If
                            End If
                        End If
                    End If
                Next
            Catch ex As Exception
                'MsgBox(ex.Message, vbOKOnly, ex.Source)
                'Return ""
            End Try

            '没有办法读取机器码
            Return "A5A5A5A5"
        End Function

        Public Function Uint32ToHEX(ID As Long) As String
            Dim text As String = Conversion.Hex(ID)
            While Len(text) < 8
                text = "0" + text
            End While
            Return text
        End Function

    End Module
End Namespace